package cn.wolfcode.reaml;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CrmRealm extends AuthorizingRealm {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    //将容器中的配置的凭证匹配器注入给当前的 Realm 对象
    // 在该 Realm 中使用指定的凭证匹配器来完成密码匹配的操作
    @Autowired
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);

    }

    //获取授权信息
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        /*SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        List<String> roles = new ArrayList<>();
        roles.add("role1");
        roles.add("role2");

        List<String> permissions = new ArrayList<>();
        permissions.add("user:list");
        permissions.add("user:delete");
        permissions.add("user:update");

        info.addRoles(roles);
        info.addStringPermissions(permissions);
        return info;*/
        System.out.println("-----------------------------------------");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); //获取到当前登录的用户
        Employee employee = (Employee) principals.getPrimaryPrincipal(); //如果是超级管理员，授予其 admin 的角色和所有权限
        if(employee.isAdmin()){
            info.addRole("ADMIN");
            info.addStringPermission("*:*");
        }else{
            //根据登录用户的 id 查询到其拥有的所有角色的编码
            List<Role> roles = roleMapper.selectByEmployeeId(employee.getId());//根据登录用户的 id 查询到其拥有的所有权限表达式
            ArrayList<String> roleSnList = new ArrayList<>();
            for (Role role : roles) {
                roleSnList.add(role.getSn());
            }
            List<String> expressions = permissionMapper.selectByEmployeeId(employee.getId());
            //将用户拥有的角色和权限添加到授权信息对象中，供 Shiro 权限校验时使用
            info.addRoles(roleSnList);
            info.addStringPermissions(expressions);
        }
        return info;
    }

    //获取认证信息
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //返回值是查询出来的数据,如果查到这个账号,就返回此账号的正确的数据,如果查不到这个账号,就返回空null
        //通过身份获取登录的用户名
        String username = (String) token.getPrincipal();
        //通过用户名去数据库查找是否存在
        Employee employee = employeeMapper.selectByName(username);
        if(!employee.isStatus()){
            throw new DisabledAccountException();
        }
        //判断是否存在数据库
        if (employee != null) {
            //身份信息,凭证信息(正确的),当前realm的名字
            return new SimpleAuthenticationInfo(employee, employee.getPassword(),
                    ByteSource.Util.bytes(employee.getName()),
                    this.getName());

        }
        return null;
    }
}
